package com.u.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.u.character.Manager;
import com.u.character.Processer;
import com.u.character.Repairer;
import com.u.character.Role;
import com.u.character.Station;
import com.u.eventInfo.Event;
import com.u.tool.EventListUtil;

import net.sf.json.JSON;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class GetEventListService
 */
@WebServlet("/GetEventListService")
public class GetEventListService extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetEventListService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		HttpSession session = request.getSession();
		Role role =  (Role) session.getAttribute("Role");
		String roleType = (String) session.getAttribute("roleType");
		String page = request.getParameter("page");
		String date = request.getParameter("date");
		String station_id = request.getParameter("station_id");
		String item_id = request.getParameter("item_id");
		String event_id = request.getParameter("search_id");
		String options = page + ","+ station_id + "," + date + "," +item_id + "," + event_id; 
		Role user = null;
		JSONArray json = new JSONArray();
		List<Event> m = new ArrayList<Event>();
		try {
			switch(roleType) {
			case("manager"):{
				user = new Manager(role);
				break;
			}
			case("processer"):{
				user = new Processer(role);
				break;
			}
			case("station"):{
				user = new Station(role);
				break;
			}
			case("repairer"):{
				user = new Repairer(role);
				break;
			}
			default:break;
			}
			m = EventListUtil.getEventList(role,options);
			json.add(m);
		}catch(NullPointerException e) {
			json.add("No role");
		}
		PrintWriter out = response.getWriter();
		out.println(json.toString());
		out.close();		
	}

}
